﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;

namespace BBMS.DLL.Forms.Catalog
{
    public partial class frmCounterUpdate : Base.FrmEditBase
    {
        public frmCounterUpdate(BBMS.DLL.Base.ObjectBase obj)
        {
            InitializeComponent();
            curObj = obj;
        }
        public new static readonly string FunctionKey = "DMQuay";
        protected override string GetFunctionKey() { return "DMQuay"; }

        
        protected override void OnLoad()
        {
            cbxNghanhHang.Items.Clear();
            if (ObjectLists.NghanhHangList.GetKeys() != null)
                foreach (object key in ObjectLists.NghanhHangList.GetKeys())
                    cbxNghanhHang.Items.Add(key);

            if (curObj != null)
            {
                txtMa.Text = ((Quay)curObj).Ma;
                txtTen.Text = ((Quay)curObj).Ten;
                txtTenTA.Text = ((Quay)curObj).TenTA;
                for (int i = 0; i < cbxNghanhHang.Items.Count; i ++)
                {
                    if (cbxNghanhHang.Items[i].ToString().Equals(((Quay)curObj).MaNganhHang))
                    {
                        cbxNghanhHang.SelectedIndex = i;
                        break;
                    }
                }
                txtTen.Focus();
                txtMa.ReadOnly = true;

                if(!Config.CurUser.HaveRole(Role.Edit,GetFunctionKey())){
                    txtMa.ReadOnly = true;
                    txtTen.ReadOnly = true;
                    txtTenTA.ReadOnly = true;
                    cbxNghanhHang.Enabled = false;
                    
                    btnSaveNew.Enabled =false;
                    btnReset.Enabled = false;
                    btnSaveClose.Enabled = false;
                }
            }
            else
            {
                txtMa.Focus();
            }
        }

        protected override ObjectBase GetEditedObject() {
            if (txtMa.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập quầy.");
                txtMa.Focus();
                return null;
            }
            if (txtTen.Text.Trim().Length == 0)
            {
                MessageBox.Show("Hãy nhập tên quầy.");
                txtTen.Focus();
                return null;
            }

            string key = cbxNghanhHang.Text;
            NganhHang obj = (NganhHang)ObjectLists.NghanhHangList.Get(key);
            if (obj == null)
            {
                MessageBox.Show("Hãy nhập chọn nghành hàng.");
                cbxNghanhHang.Focus();
                return null;
            }

            return new Quay(txtMa.Text.Trim().ToUpper(),txtTen.Text.Trim(),txtTenTA.Text.Trim(),obj.Ma);
        }

        protected override void Reset()
        {
            txtMa.ReadOnly = false;
            txtMa.Text = "";
            txtTen.Text = "";
            txtTenTA.Text = "";
            cbxNghanhHang.SelectedIndex = -1;
            txtMa.Focus();

            if (curObj != null) OnLoad();
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnClose();
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        
        private void btnSaveNew_Click(object sender, EventArgs e)
        {
            OnSaveAndNew();
        }

        private void btnSaveClose_Click(object sender, EventArgs e)
        {
            OnSaveAndClose();
        }

        private void cbxNghanhHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            string key = cbxNghanhHang.Text;
            NganhHang obj = (NganhHang)ObjectLists.NghanhHangList.Get(key);
            if (obj != null)
            {
                txtTenNghanhHang.Text = obj.Ten;
            }
        }

        private void cbxNghanhHang_Leave(object sender, EventArgs e)
        {
            string key = cbxNghanhHang.Text;
            NganhHang obj = (NganhHang)ObjectLists.NghanhHangList.Get(key);
            if (obj != null)
            {
                txtTenNghanhHang.Text = obj.Ten;
            }
            else
            {
                txtTenNghanhHang.Text = "";
                cbxNghanhHang.Text = "";
                cbxNghanhHang.SelectedIndex = -1;
                MessageBox.Show("Không tìm thấy mã nghành hàng tương ứng trong cơ sở dữ liệu.");
            }
        }
        
    }
}
